package com.boot.modules.core.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.boot.common.result.Result;
import com.boot.modules.core.mapper.CoreMapper;
import com.boot.modules.core.service.CoreService;
import com.boot.system.entity.SysModule;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service("coreService")
public class CoreServiceImpl extends ServiceImpl<CoreMapper, Row> implements CoreService {

    @Override
    public Page<Map<String, Object>> getCorePage(SysModule sysModule, Map<String, Object> queryParam) {
        // 动态表名
        String tableName = StrUtil.format("tb_{}", StrUtil.toUnderlineCase(sysModule.getMid()));
        // 基础参数
        int pageNum = (int) queryParam.getOrDefault("pageNum", 1);
        int pageSize = (int) queryParam.getOrDefault("pageSize", 10);
        // 查询条件
        QueryWrapper query = QueryWrapper.create().from(tableName);
        query.orderBy((String) queryParam.getOrDefault("orderBy", "update_time desc, id desc"));
        Page<Row> page = this.page(new Page<>(pageNum, pageSize), query);
        // 动态解析表结构
        List<Map<String, Object>> tableList = new ArrayList<>();
        JSONArray tableJson = JSONUtil.parseArray(sysModule.getTableJson());
        for (Row row : page.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", row.get("id"));
            for (int i = 0; i < tableJson.size(); i++) {
                // 解析字段
                JSONObject field = tableJson.getJSONObject(i);
                map.put(field.get("name").toString(), row.get(field.get("name").toString()));
            }
            tableList.add(map);
        }
        return new Page<>(tableList, page.getPageNumber(), page.getPageSize(), page.getTotalRow());
    }

    @Override
    public List<Map<String, Object>> getCoreList(SysModule sysModule, Map<String, Object> queryParam) {
        return List.of();
    }

    @Override
    public Result<?> addCore(SysModule sysModule, Map<String, Object> dataParam) {
        return null;
    }

    @Override
    public Result<?> updateCore(SysModule sysModule, Map<String, Object> dataParam) {
        return null;
    }

    @Override
    public boolean deleteCore(SysModule sysModule, String ids) {
        return false;
    }

    @Override
    public List<Map<String, Object>> getCacheList(SysModule sysModule) {
        return List.of();
    }

    @Override
    public Map<String, Object> getCache(SysModule sysModule, Long id) {
        return Map.of();
    }

    @Override
    public void setCache(SysModule sysModule, Long id) {

    }

    @Override
    public void deleteCache(SysModule sysModule, List<Long> ids) {

    }
}
